<script lang="ts" setup>
import { useForm } from '@/hooks/web/useForm'
import { FormSchema } from '@/types/form'
import * as EmployeeApi from '@/api/train/employee/index'
import { ref, reactive } from 'vue'
import { useI18n } from '@/hooks/web/useI18n'
import Form from '@/components/Form/src/Form.vue'

const message = useMessage()
const { t } = useI18n()

const dialogVisible = ref(false)
const dialogTitle = ref('新增员工')
const formLoading = ref(false)
const formType = ref<'create' | 'update'>('create')

const schema = reactive<FormSchema[]>([
  {
    field: 'name',
    label: '员工名称',
    value: '',
    component: 'Input',
    formItemProps: {
      rules: [{ required: true, message: '请输入员工名称' }]
    }
  },
  {
    field: 'professional',
    label: '员工职称',
    value: '',
    component: 'Input',
    formItemProps: {
      rules: [{ required: true, message: '请输入员工名称' }]
    }
  },
  {
    field: 'avatar',
    label: '员工头像',
    value: '',
    component: 'UploadImg',
    formItemProps: {
      rules: [{ required: true, message: '请上传员工头像' }]
    }
  },
  {
    field: 'type',
    label: '员工类型',
    value: '1',
    component: 'Select',
    componentProps: {
      options: [
        { label: '校领导', value: '1' },
        { label: '教练', value: '2' },
        { label: '客服', value: '3' }
      ]
    },
    formItemProps: {
      rules: [{ required: true, message: '请选择员工类型' }]
    }
  },
  {
    field: 'detail',
    label: '员工介绍',
    component: 'Input'
  }
])

const { register, elFormRef, methods: formMethods } = useForm()

const open = (type: 'create' | 'update', id?: string) => {
  dialogVisible.value = true
  dialogTitle.value = type === 'update' ? '编辑员工' : '新增员工'
  formType.value = type

  if (type === 'update') {
    formLoading.value = true
    EmployeeApi.getEmployeeById(id!).then((data) => {
      console.log('update: ', data)
      formMethods.setValues(data)
      formLoading.value = false
    })
  }
}
defineExpose({ open })
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!elFormRef.value) return
  const valid = await elFormRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true

  try {
    const formData = (await formMethods.getFormData()) || {}

    if (formType.value === 'create') {
      console.log('创建banner：', formData)
      await EmployeeApi.employeeCreate(formData)
      message.success(t('common.createSuccess'))
    } else {
      console.log('更新banner：', formData)
      await EmployeeApi.updateEmployee(formData)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>

<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="700">
    <Form :schema="schema" @register="register" />
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
